package com.ilibrary.service.impl;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;

import com.ilibrary.base.MyBaseVO;
import com.ilibrary.constants.MyConstants;
import com.ilibrary.constants.MyMsgConstants;
import com.ilibrary.db.dao.hibernate.NewsCategoryDao;
import com.ilibrary.db.dao.hibernate.NewsDao;
import com.ilibrary.db.entity.News;
import com.ilibrary.db.entity.NewsCategory;
import com.ilibrary.service.NewsService;
import com.ilibrary.web.dto.ResultDto;
import com.ilibrary.web.vo.NewsCategoryVO;
import com.ilibrary.web.vo.NewsVO;

public class NewsServiceImpl implements NewsService {
	
	private NewsDao newsDao;
	private NewsCategoryDao newsCategoryDao;

	public NewsDao getNewsDao() {
		return newsDao;
	}
	public void setNewsDao(NewsDao newsDao) {
		this.newsDao = newsDao;
	}
	public NewsCategoryDao getNewsCategoryDao() {
		return newsCategoryDao;
	}
	public void setNewsCategoryDao(NewsCategoryDao newsCategoryDao) {
		this.newsCategoryDao = newsCategoryDao;
	}
	
	@Override
	public ResultDto<MyBaseVO> add(NewsVO newsVO) {
		ResultDto<MyBaseVO> resultDto = new ResultDto<MyBaseVO>();
		News news =  new News();
		try {
			BeanUtils.copyProperties(news, newsVO);
			news.setNewsCategory(newsCategoryDao.findById(newsVO.getNewsCategoryId()));
		} catch (IllegalAccessException e) {
			e.printStackTrace();
			resultDto.setSuccess(false);
			resultDto.setMsg(MyMsgConstants.ADD_EXCEPTION);
			e.printStackTrace();
			return resultDto;
		} catch (InvocationTargetException e) {
			e.printStackTrace();
			resultDto.setSuccess(false);
			resultDto.setMsg(MyMsgConstants.ADD_EXCEPTION);
			e.printStackTrace();
			return resultDto;
		}
		newsDao.add(news);
		resultDto.setSuccess(true);
		return resultDto;
	}
	
	@Override
	public ResultDto<NewsVO> queryList(int start, int limit, String newsCategoryId, String keywords) {
		ResultDto<NewsVO> resultDto = new ResultDto<NewsVO>();
		NewsVO newsVO = null;
		NewsCategory newsCategory = null;
		
		List<News> newsList = newsDao.queryList(start, limit, newsCategoryId, keywords);
		Integer total = newsDao.queryCnt(newsCategoryId, keywords);
		
		for (News news : newsList) {
			newsVO = new NewsVO();
			try {
				BeanUtils.copyProperties(newsVO, news);
				newsCategory = newsCategoryDao.findById(news.getNewsCategory().getNewsCategoryId());
				newsVO.setNewsCategoryId(newsCategory.getNewsCategoryId());
				newsVO.setNewsCategoryName(newsCategory.getNewsCategoryName());
				resultDto.addToResultList(newsVO);
			} catch (IllegalAccessException e) {
				e.printStackTrace();
				resultDto.setSuccess(false);
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
				resultDto.setSuccess(false);
				e.printStackTrace();
			}
		}
		resultDto.setTotal(total);
		resultDto.setSuccess(true);
		return resultDto;
	}
	
	@Override
	public ResultDto<NewsVO> findById(int newsId) {
		ResultDto<NewsVO> resultDto = new ResultDto<NewsVO>();
		News news = newsDao.findById(newsId);
		NewsVO newsVO = null;
		if(news != null) {
			newsVO = new NewsVO();
			try {
				BeanUtils.copyProperties(newsVO, news);
				NewsCategory newsCategory = newsCategoryDao.findById(news.getNewsCategory().getNewsCategoryId());
				newsVO.setNewsCategoryId(newsCategory.getNewsCategoryId());
				newsVO.setNewsCategoryName(newsCategory.getNewsCategoryName());
				resultDto.addToResultList(newsVO);
			} catch (IllegalAccessException e) {
				e.printStackTrace();
				resultDto.setMsg(MyMsgConstants.QUERY_EXCEPTION);
				resultDto.setSuccess(false);
				return resultDto;
			} catch (InvocationTargetException e) {
				e.printStackTrace();
				resultDto.setMsg(MyMsgConstants.QUERY_EXCEPTION);
				resultDto.setSuccess(false);
				return resultDto;
			}
			resultDto.setSuccess(true);
			return resultDto;
		}else {
			resultDto.setMsg(MyMsgConstants.NEWS_NOT_EXIST);
			resultDto.setSuccess(false);
			return resultDto;
		}
	}
	
}
